package main

import (
	"fmt"
	"net"
)

func main() {
	// 1. 监听端口
	udpConn, err := net.ListenUDP("udp", &net.UDPAddr{ //server端监听端口信息
		IP:   net.IPv4(127, 0, 0, 1),
		Port: 9090,
	})
	if err != nil {
		fmt.Printf("监听失败,%v\n", err)
		return
	}
	for {
		var data [1024]byte                          //数组
		n, addr, err := udpConn.ReadFromUDP(data[:]) //参数是slice，addr是client端地址
		if err != nil {
			fmt.Printf("读取失败:%v\n", err)
			continue
		}
		fmt.Printf("接收到：%v---%v\n", string(data[:n]), addr)

		_, err = udpConn.WriteToUDP([]byte("服务端收到"), addr)
		if err != nil {
			fmt.Printf("发送失败:%v\n", err)
			continue
		}

	}

}
